package com.jaya.budan.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.activity.Topic_Detail_Activity;
import com.jaya.budan.adapter.Topic_ListViewAdapter;
import com.jaya.budan.base.BaseFragment;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyListView;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class TopicFragment extends BaseFragment implements View.OnClickListener {

    private MyListView main_listview;
    private PullToRefreshLayout topic_pull_layout;
    private String access_token;
    private List<User_Limit> user_limitList;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_topic, container, false);
        main_listview = view.findViewById(R.id.main_listview);
        topic_pull_layout = view.findViewById(R.id.topic_pull_layout);
        Bundle bundle = getArguments();
        access_token = bundle.getString("access_token");
        postData();
        topic_pull_layout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                topic_pull_layout.finishRefresh();//结束刷新
            }

            @Override
            public void loadMore() {
                topic_pull_layout.finishLoadMore();//结束加载更多
            }
        });
        main_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), Topic_Detail_Activity.class);
                intent.putExtra("access_token", access_token);
                intent.putExtra("user_limit", user_limitList.get(position));
                startActivity(intent);
            }
        });
        return view;
    }

    public static TopicFragment newInstance() {
        Bundle args = new Bundle();
        TopicFragment fragment = new TopicFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_main_shaixuan:
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.topic_qz_list_Url + access_token, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("topic_main", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        user_limitList = gson.fromJson(info.toString(), new TypeToken<List<User_Limit>>(){}.getType());
                        Log.d("topic_main",JSON.toJSONString(user_limitList));
                        main_listview.setAdapter(new Topic_ListViewAdapter(getActivity(), user_limitList, null, 1));
                    } else {
                        ToolUtils.showToast(getActivity(), message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(getActivity(), "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(getActivity()).add(request);
    }
}
